package view.GUI.adminView.invoiceOverview;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import view.GUI.mainView.MainView;
import model.Utility;
import model.data.tableModel.sale.Bill;
import model.handler.fileHandling.FileHandler;
import model.invoicing.DateHandler;
import enums.GUIText;
import enums.Status;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Overview extends GridPane {

	private Label lblTitle; /** image by  http://www.visualpharm.com */
	private Separator sepTop;
	private Separator sepBottom;
	private Separator sepMid;
	private Button btnCancel;
	private Label lblActualMonth;
	private Label lblActiveDebt;
	private Button btnSave;
	private List<Bill> billList;
	private Stage primaryStage;
	
	private int rowCounter = 0;

	public Overview(List<Bill> list, Stage primaryStage) {
		super();
		billList = new ArrayList<>();
		createWidgets();
		addWidgets(list);
		setupLayout();
		setupInteraction();
		this.primaryStage = primaryStage;
	}

	private void setupInteraction() {
		btnSave.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent event) {
				saveBills();
			}
		});
	}

	private void addActiveDebt(String name, Bill bill) {
		billList.add(bill);
		Label lblName = new Label(name);
		Hyperlink lblInvoice = new Hyperlink(Utility.concatStrings("(", GUIText.INVOICE.toString(), ")"));
		Label lblPaid = new Label(GUIText.PAID.toString());
		Label lblAmount = new Label(MainView.formatNumber(bill.getAmount()));
		Label lblDate = new Label(DateHandler.formatHeaderDate(DateHandler.parseMonthDateStringToDate(bill.getDate())));
		CheckBox checkPaid = new CheckBox();
		int rowCount = rowCounter++;
		
		lblInvoice.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				FileHandler.openPdf(bill.getFile());
			}
			
		});
		
		checkPaid.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				bill.setStatus(Status.PAID);
			}
		});
		lblAmount.getStyleClass().addAll("highlight", "double-underline");
		lblName.getStyleClass().add("highlight");

		this.add(lblName, 0, rowCount);
		this.add(lblInvoice, 1, rowCount);
		this.add(lblDate, 2, rowCount);
		this.add(lblAmount, 3, rowCount);
		this.add(checkPaid, 4, rowCount);
		this.add(lblPaid, 5, rowCount);
	}
	
	public void saveBills(){
		for(Bill b: billList){
			b.writeUpdateToDB();
		}
		primaryStage.close();
	}

	private void createWidgets() {
		sepMid = new Separator();
		lblTitle = new Label(GUIText.OVERVIEW.toString());
		sepTop = new Separator();
		sepBottom = new Separator();
		btnCancel = new Button(GUIText.CANCEL.toString());
		lblActualMonth = new Label(DateHandler.formatHeaderDate(DateHandler.getMonthBeforeDate(new Date())));
		lblActiveDebt = new Label(GUIText.ACTIVEDEBT.toString());
		btnSave = new Button(GUIText.SAVE.toString());
	}

	private void addWidgets(List<Bill> list) {
		this.add(lblTitle, 0, rowCounter++);
		this.add(sepTop, 0, rowCounter++);
		this.add(lblActiveDebt, 0, rowCounter++);
		this.add(lblActualMonth, 0, rowCounter++);
		this.add(sepMid, 0, rowCounter++);
		
		for(Bill b: list){
			addActiveDebt(b.getCustomer().getName(), b);
		}
		this.add(btnSave, 0, rowCounter);
		this.add(btnCancel, 1, rowCounter++);
		this.add(sepBottom, 0, rowCounter++);
	}

	private void setupLayout() {
		lblActualMonth.setId("subtopic");
		lblActiveDebt.setId("subtopic");
		GridPane.setColumnSpan(lblActiveDebt, 5);
		GridPane.setColumnSpan(lblActualMonth, 5);
		lblTitle.setId("topic");
		this.getStyleClass().add("grid");
		GridPane.setColumnSpan(sepBottom, 7);
		GridPane.setColumnSpan(sepTop, 7);
	}
}
